//
//  RestApplicationViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-24.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "RestApplicationViewController.h"
#import "UIAlertView+Kirinrin.h"
#import "RestListViewController.h"
#import <MMDrawerController/MMDrawerBarButtonItem.h>
#import <UIViewController+MMDrawerController.h>
#include "MenuViewController.h"
#import <TSMessage.h>
#import "NetworkAccessManager.h"

@interface RestApplicationViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak,nonatomic) AppDelegate *appDelegate;
@end

@implementation RestApplicationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.textView.contentInset = UIEdgeInsetsMake(-60.0, 0, 0, 0);
    self.appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (!self.navigationController || self.navigationController.viewControllers[0]==self) {
        MMDrawerBarButtonItem *leftDrawerButton = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(leftDrawerButtonPress:)];
        [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
    }
    
    [TSMessage setDefaultViewController:self];
}
-(void)leftDrawerButtonPress:(id)sender{
    [self.view endEditing:YES];
//   MMDrawerController *mm_customerdrawer = [[MMDrawerController alloc] initWithCenterViewController:[[RestApplicationViewController alloc]init] leftDrawerViewController:[[MenuViewController alloc]init]];
     NSLog(@"MMDrawerController--->%@",self.mm_drawerCustomerController.centerViewController);
    NSLog(@"MMDrawerController--->%@",self.mm_drawerCustomerController.leftDrawerViewController);
//    [mm_customerdrawer toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    [self.mm_drawerCustomerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
//-(MMDrawerController*)mm_drawerCustomerController{
//    UIViewController *parentViewController = self.parentViewController;
//    while (parentViewController != nil) {
//        if([parentViewController isKindOfClass:[MMDrawerController class]]){
//            return (MMDrawerController *)parentViewController;
//        }
//        parentViewController = parentViewController.parentViewController;
//    }
//    return nil;
//}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.view endEditing:YES];
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//    if ([segue.identifier isEqualToString:@"toRestList"]) {
//        
////        RestListViewController *destViewControll = [segue destinationViewController];
////        destViewControll.noShowMenuButton = YES;
//    }
//}

- (IBAction)sendAction:(id)sender {
    
    
    
    NSString *textcontent = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (textcontent.length == 0) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildLength];
        return;
    }
    
    self.view.userInteractionEnabled = NO;
    
    NSString *urlString = @"RestApplication.json";
    NSDictionary *parameters = @{@"key":self.appDelegate.accessKey, @"content":textcontent};
    
    [[NetworkAccessManager shareManager]POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            self.textView.text = @"";
//            [self performSegueWithIdentifier:@"toRestList" sender:nil];
//            [self dismissViewControllerAnimated:YES completion:nil];
            [self performSegueWithIdentifier:@"ToRestList" sender:sender];
            if ([self.delegate respondsToSelector:@selector(reloadViewRestDelegate:)]) {
                [self.delegate reloadViewRestDelegate:self];
            }
            
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        self.view.userInteractionEnabled = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        self.view.userInteractionEnabled = YES;
        [UIAlertView alertWithNetworkError];
    }];
  
}

- (IBAction)touchBackgound:(id)sender {
    [self.view endEditing:YES];
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([self.textView.text isEqualToString:@"提醒信息"]) {
        [self.textView setText:@""];
    }
    return YES;
}
- (IBAction)doBackList:(id)sender {
//    NSLog(@"ceshi^^^^^^^^^");
//    for (UIViewController *viewController in self.navigationController.viewControllers) {
//        NSLog(@"ceshi^^^^^^^^^%@",viewController);
//    }
//    [self.navigationController popToViewController:[self.navigationController.viewControllers lastObject] animated:NO];
}
//- (IBAction)cancelView:(id)sender {
//    [self dismissViewControllerAnimated:YES completion:nil];
//}
@end
